<?php
/* ******************************
 *	transactions aJax actions
 * ******************************
 */
 
require_once '../../includes/classes/core.php';

$action 	= $_REQUEST['act'];
$user_id	= $_COOKIE['USERID'];
$error 		= '';
$data 		= '';
	
switch ($action) {
	case 'get_edit_page':
		$incom_id				= $_REQUEST['id'];
		$page		= GetPage();
		$data		= array('page'	=> $page);

		break;
		
    case 'get_list':
	    $count = $_REQUEST['count'];
	    $hidden = $_REQUEST['hidden'];
	    
        $rResult = mysql_query("SELECT 		sale_transaction.id,
        									sale_transaction.start_date,
											sale_transaction.sum_cost,
											sale_transaction_status.description
								FROM 		sale_transaction
        		
								LEFT JOIN 	sale_transaction_status ON sale_transaction.`status` = sale_transaction_status.id
								WHERE 		sale_transaction.`status` = 0
								ORDER BY 	sale_transaction.`status` ASC");
		
		$data = array(
			"aaData"	=> array()
		);
		
		while ( $aRow = mysql_fetch_array( $rResult ) )
		{
			$row = array();
			for ( $i = 0 ; $i < $count ; $i++ )
			{
				/* General output */
				$row[] = $aRow[$i];
				if($i == ($count - 1)){
					$row[] = '<input type="checkbox" name="check_' . $aRow[$hidden] . '" class="check" value="' . $aRow[$hidden] . '" />';
				}
			}
			$data['aaData'][] = $row;
		}
		
        break;
    case 'disable':
		$trans_id = $_REQUEST['id'];
		
		SaleunDone($trans_id);
		
		DisableProduct($trans_id);
		
		break;
    default:
       $error = 'Action is Null';
}

$data['error'] = $error;

echo json_encode($data);


/* ******************************
 *	transactions Functions
 * ******************************
 */

function DisableProduct($trans_id){
	
	 $rResult = mysql_query("UPDATE  sale_transaction
							 SET     sale_transaction.`status` = 3
							 WHERE   sale_transaction.id       = $trans_id ");
}

function SaleunDone($trans_id){
	$res1 = mysql_query("SELECT sale_transaction_detail.id as detail_id,
							    sale_transaction_detail.product_quantity,
							    sale_transaction_detail.production_id
						 FROM   sale_transaction_detail
						 WHERE  sale_transaction_detail.transaction_id=$trans_id 
						 AND sale_transaction_detail.status != 3" ); 
	
	while ($row1 = mysql_fetch_array($res1)) {
		
		$req_quantity = $row1[product_quantity];
		$prod_id	  = $row1[production_id];
			
		$res = mysql_query("SELECT   `reserve`.id,
									 `reserve`.`quantity`,
									 `reserve`.`sold`
							FROM 	 `reserve`
							WHERE 	 `reserve`.`production_id` = $prod_id
							ORDER BY `reserve`.`id` desc");

		while ($row = mysql_fetch_array($res)) {
			
			if ($req_quantity != 0 && $row['sold']!=0) {
				if($row['sold'] >= $req_quantity){
					$sum = $row['sold'] - $req_quantity;
				
					mysql_query("UPDATE	`reserve`
								 SET	`sold` = $sum
								 WHERE	`id`   = $row[id]");
					$req_quantity = 0;
				}else{
					$sum = 0;
					mysql_query("UPDATE	`reserve`
								 SET	`sold` = $sum
								 WHERE	`id`   = $row[id]" );
					$req_quantity = $req_quantity - $row['sold'];
				}
				mysql_query("UPDATE  sale_transaction_detail
							 SET     sale_transaction_detail.`status` = 3
							 WHERE   sale_transaction_detail.id       = $row1[detail_id];");
			}
		}
	}
}

function GetPage(){
	$data  .= '
		
	<div >
			<div style="float: left; width: 100%;">
				<fieldset>
				<div>
            	<h2 align="center">ტრანზაქციები</h2>
            	 <div id="button_area">
        			<button id="delete_button1_1">გაუქმება</button>
        		</div>
                <table class="display" id="example11_1">
                    <thead>
                        <tr id="datatable_header">
                            <th>ID</th>
                            <th style="width: 100%;">თარიღი</th>
							<th class="min">კოდი</th>
                            <th class="min">დასახელება</th>
                            <th class="min">რაოდენობა</th>
                            <th class="min">ფასი</th>
                            <th class="min">სტატუსი</th>
                            <th class="check">#</th>
                        </tr>
                    </thead>
                    <thead>
                        <tr class="search_header">
                            <th class="colum_hidden">
                            	<input type="text" name="search_id" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_browser" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_browser" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_browser" value="ფილტრი" class="search_init" />
                            </th>
							<th>
                                <input type="text" name="search_browser" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_browser" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                                <input type="text" name="search_browser" value="ფილტრი" class="search_init" />
                            </th>
                            <th>
                            	<input type="checkbox" name="check-all" id="check-all">
                            </th>
                        </tr>
                    </thead>
                </table>
            </div>
				</fieldset>
				<div id="additional_info">
	  		</div>
	  				<table/>
			</div>
	  		<input type="hidden" id="tarns_hid_id" value="'.$_REQUEST['id'].'"/>
    </div>';

	return $data;
}
?>